﻿Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports Microsoft.Reporting.WebForms
Imports log4net

Partial Class Report_RP09
    Inherits System.Web.UI.Page

    Dim Report As Object
    Public ScriptText As String
    Private Shared logger As ILog = LogManager.GetLogger("Report_RP09")

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Session("users") Is Nothing Then
            ScriptText = "<script> wclose();</script>"
        End If
    End Sub
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        'If Session("fundcenterS") = "" Then
        '    Session("fundcenterS") = "0011"
        '    Session("fundcenterE") = "0012"

        'End If

        'If Session("users") = "" Then
        '    Session("users") = "wassana"
        'End If
        'Session("year") = 2556
        'Session("version") = 2

        If Session("users") Is Nothing Then
            Response.Redirect("../LoginForm.aspx")
        End If


        Getdata()


    End Sub


    Function Getdata() As Decimal
        Dim dt As New DataTable
        dt = GetData2(Val(Session("year1")), Session("version1"), Val(Session("year2")), Session("version2"), Val(Session("year3")), Session("version3"), Val(Session("year4")), Session("version4"), Val(Session("year5")), Session("version5"))

        ' dt = GetData2(2557, "1", 2557, "2", 2557, "3", 2557, "A01", 2557, "B01")
        dt.TableName = "R09DT"
        Dim das As New DataSet
        das.DataSetName = "R01DS"
        das.Tables.Add(dt)
        ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Local

        ReportViewer1.LocalReport.ReportPath = "Report\R09.rdlc"
        'If Val(Request("a")) = 0 Then
        '    ReportViewer1.LocalReport.ReportPath = "Report\R09.rdlc"
        'ElseIf Val(Request("a")) = 1 Then
        '    ReportViewer1.LocalReport.ReportPath = "Report\R09_1.rdlc"
        'ElseIf Val(Request("a")) = 2 Then
        '    ReportViewer1.LocalReport.ReportPath = "Report\R09_2.rdlc"
        'ElseIf Val(Request("a")) = 3 Then
        '    ReportViewer1.LocalReport.ReportPath = "Report\R09_3.rdlc"
        'End If

        ReportViewer1.LocalReport.DataSources.Clear()
        ReportViewer1.LocalReport.DataSources.Add(New Microsoft.Reporting.WebForms.ReportDataSource("DataSet1", das.Tables("R09DT")))
        ReportViewer1.DocumentMapCollapsed = True

        Dim UserName As String = Session("users")
        Dim years As String = Session("year")
        Dim version As String = Session("version")
        Dim users As String = Session("users")

        Dim a As String = Val(Request("a"))

        '=======================================================================================
        Dim year1 As String = "แผน V" & Session("version1") & " ปี " & Session("year1")
        Dim year2 As String = "แผน V" & Session("version2") & " ปี " & Session("year2")
        Dim year3 As String = "แผน V" & Session("version3") & " ปี " & Session("year3")
        Dim year4 As String = "แผน V" & Session("version4") & " ปี " & Session("year4")
        Dim year5 As String = "แผน V" & Session("version5") & " ปี " & Session("year5")

        Dim p1, p2, p3, y1, y2, y3, y4, y5, Apara As ReportParameter

        p1 = New ReportParameter("year", years)
        p2 = New ReportParameter("version", version)
        p3 = New ReportParameter("username", UserName)

        If Session("version1") <> "" And Session("year1") <> "" Then
            y1 = New ReportParameter("year1", year1)
        Else
            y1 = New ReportParameter("year1", " ")
        End If

        If Session("version2") <> "" And Session("year2") <> "" Then
            y2 = New ReportParameter("year2", year2)
        Else
            y2 = New ReportParameter("year2", " ")
        End If

        If Session("version3") <> "" And Session("year3") <> "" Then
            y3 = New ReportParameter("year3", year3)
        Else
            y3 = New ReportParameter("year3", " ")
        End If

        If Session("version4") <> "" And Session("year4") <> "" Then
            y4 = New ReportParameter("year4", year4)
        Else
            y4 = New ReportParameter("year4", " ")
        End If

        If Session("version5") <> "" And Session("year5") <> "" Then
            y5 = New ReportParameter("year5", year5)
        Else
            y5 = New ReportParameter("year5", " ")
        End If

        '=======================================================================================

        Apara = New ReportParameter("a", Val(Request("a")))

        ReportViewer1.LocalReport.SetParameters(p1)
        ReportViewer1.LocalReport.SetParameters(p2)
        ReportViewer1.LocalReport.SetParameters(p3)


        ReportViewer1.LocalReport.SetParameters(y1)
        ReportViewer1.LocalReport.SetParameters(y2)
        ReportViewer1.LocalReport.SetParameters(y3)
        ReportViewer1.LocalReport.SetParameters(y4)
        ReportViewer1.LocalReport.SetParameters(y5)

        ReportViewer1.LocalReport.SetParameters(Apara)
        ReportViewer1.LocalReport.Refresh()


        'Response.Write(dt.Rows.Count & sql)
        Session("count") = dt.Rows.Count



        Return dt.Rows.Count

    End Function

    Function GetData2(planyear1 As Decimal, version1 As String, planyear2 As Decimal, version2 As String, planyear3 As Decimal, version3 As String, planyear4 As Decimal, version4 As String, planyear5 As Decimal, version5 As String) As DataTable

        Dim dt As New DataTable
        dt.Columns.Add("fundcentercode", Type.GetType("System.String"))
        dt.Columns.Add("cmmtcode", Type.GetType("System.String"))
        dt.Columns.Add("Description", Type.GetType("System.String"))

        dt.Columns.Add("ProductCode", Type.GetType("System.String"))
        dt.Columns.Add("Description1", Type.GetType("System.String"))

        dt.Columns.Add("amount1", Type.GetType("System.Decimal"))
        dt.Columns.Add("amount2", Type.GetType("System.Decimal"))
        dt.Columns.Add("amount3", Type.GetType("System.Decimal"))
        dt.Columns.Add("amount4", Type.GetType("System.Decimal"))
        dt.Columns.Add("amount5", Type.GetType("System.Decimal"))
        dt.Columns.Add("amount_diff", Type.GetType("System.Decimal"))
        dt.Columns.Add("amount_diff_per", Type.GetType("System.Decimal"))
        Dim adapter As New Data_ReportTableAdapters.R09TableAdapter
        Dim datatable As New Data_Report.R09DataTable
        Dim sql As String
        sql = " and 1 = 1 "

        If Session("FCTRmode") = "1" Then
            sql += " and  BudgetDetails.FundCenter_FundCenterCode IN (" & Session("FundcenterAll") & ") "
        ElseIf Session("FCTRmode") = "2" Then
            sql += " and  BudgetDetails.FundCenter_FundCenterCode IN (" & Session("FundcenterAll") & ") "
        End If

        sql += " and  BudgetDetails.PlanYear  = " & Session("year") & " and Version = '" & Session("version") & "'"
        sql += " and Cmmts.PlanYear = " & Session("year")
        sql += " and Products.PlanYear = " & Session("year")


        If Session("CmmtMode") = "1" Then
            If Val(Session("CmmtCodeS")) <> 0 And Val(Session("CmmtCodeE")) <> 0 Then
                sql += " and  Cmmts.CmmtCode  between " & Session("CmmtCodeS") & " and " & Session("CmmtCodeE")
            End If
            If Val(Session("CmmtCodeS")) <> 0 And Val(Session("CmmtCodeE")) = 0 Then
                sql += " and  Cmmts.CmmtCode = " & Session("CmmtCodeS")
            End If
            If Val(Session("CmmtCodeS")) = 0 And Val(Session("CmmtCodeE")) <> 0 Then
                sql += " and  Cmmts.CmmtCode = " & Session("CmmtCodeE")
            End If
            If Val(Session("CmmtCodeS")) = 0 And Val(Session("CmmtCodeE")) = 0 Then
                sql += " and  Cmmts.CmmtCode  between " & "'00000000'" & " and " & "'99999999'"
            End If
        ElseIf Session("CmmtMode") = "2" Then
            If Session("CmmtParent") <> "" And Session("CmmtParent") <> "0" Then
                sql += "and cmmtcode in (select cmmt_cmmtcode from  cmmthierarchies where parentcmmthierarchy_id in (select id from cmmthierarchies "
                sql += " where id = " & Session("CmmtParent") & " or parentcmmthierarchy_id = " & Session("CmmtParent") & ")"
                sql += " or id in (select id from cmmthierarchies "
                sql += " where id = " & Session("CmmtParent") & " or parentcmmthierarchy_id = " & Session("CmmtParent") & ")"
                sql += ")"
            End If
        End If

        If Val(Session("ProductS")) <> 0 And Val(Session("ProductE")) <> 0 Then
            sql += " and budgetdetails.Product_ProductCode between '" & Session("ProductS") & "' and '" & Session("ProductE") & "'"
        End If
        If Val(Session("ProductS")) <> 0 And Val(Session("ProductE")) = 0 Then
            sql += " and  budgetdetails.Product_ProductCode = '" & Session("CmmtCodeS") & "'"
        End If
        If Val(Session("ProductS")) = 0 And Val(Session("ProductE")) <> 0 Then
            sql += " and  budgetdetails.Product_ProductCode = '" & Session("CmmtCodeE") & "'"
        End If
        If Val(Session("ProductS")) = 0 And Val(Session("ProductE")) = 0 Then
            sql += " and  budgetdetails.Product_ProductCode  between " & "'00000000'" & " and " & "'99999999'"
        End If

        'sql += " and budgetdetails.PlanYear =2557"
        'sql += " and Cmmts.PlanYear =2557"
        'sql += " and products.PlanYear =2557"


        'เพิ่ม SQL filter ตามหน้าจอ
        adapter.GetDataByFilter(datatable, sql)

        'Response.Write(datatable.Count)
        Dim FundCenterCode, CmmtCode, Description, ProductCode, Description1 As String
        Dim Amount1, Amount2, Amount3, Amount4, Amount5, Amount_Diff, Amount_Diff_Per As Decimal
        Dim r As DataRow
        For Each row As Data_Report.R09Row In datatable

            FundCenterCode = row.fundcenter
            CmmtCode = row.cmmtCode
            Description = row.description

            ProductCode = row.ProductCode
            Description1 = row.Description1

            Amount1 = Val(adapter.GetSum(FundCenterCode, CmmtCode, planyear1, version1, ProductCode))
            Amount2 = Val(adapter.GetSum(FundCenterCode, CmmtCode, planyear2, version2, ProductCode))
            Amount3 = Val(adapter.GetSum(FundCenterCode, CmmtCode, planyear3, version3, ProductCode))
            Amount4 = Val(adapter.GetSum(FundCenterCode, CmmtCode, planyear4, version4, ProductCode))
            Amount5 = Val(adapter.GetSum(FundCenterCode, CmmtCode, planyear5, version5, ProductCode))


            If Session("version1") <> "" And Session("year1") <> "" Then
                Amount_Diff = 0
                Amount_Diff_Per = 0
            End If
            If Session("version2") <> "" And Session("year2") <> "" Then
                Amount_Diff = Amount2 - Amount1
                Try
                    Amount_Diff_Per = (100 * Math.Abs(Amount2 - Amount1)) / Amount1
                Catch ex As Exception
                    logger.Error(ex.Message)
                    logger.Error(ex.StackTrace)
                End Try
            End If
            If Session("version3") <> "" And Session("year3") <> "" Then
                Amount_Diff = Amount3 - Amount2
                Try
                    Amount_Diff_Per = (100 * Math.Abs(Amount3 - Amount2)) / Amount2
                Catch ex As Exception
                    logger.Error(ex.Message)
                    logger.Error(ex.StackTrace)
                End Try
            End If
            If Session("version4") <> "" And Session("year4") <> "" Then
                Amount_Diff = Amount4 - Amount3
                Try
                    Amount_Diff_Per = (100 * Math.Abs(Amount4 - Amount3)) / Amount3
                Catch ex As Exception
                    logger.Error(ex.Message)
                    logger.Error(ex.StackTrace)
                End Try
            End If
            If Session("version5") <> "" And Session("year5") <> "" Then
                Amount_Diff = Amount5 - Amount4
                Try
                    Amount_Diff_Per = (100 * Math.Abs(Amount5 - Amount4)) / Amount4
                Catch ex As Exception
                    logger.Error(ex.Message)
                    logger.Error(ex.StackTrace)
                End Try
            End If

            r = dt.NewRow()
            r("fundcentercode") = FundCenterCode
            r("cmmtcode") = CmmtCode
            r("Description") = Description

            r("ProductCode") = ProductCode
            r("Description1") = Description1

            r("amount1") = Amount1
            r("amount2") = Amount2
            r("amount3") = Amount3
            r("amount4") = Amount4
            r("amount5") = Amount5
            r("amount_diff") = Amount_Diff
            r("amount_diff_per") = Amount_Diff_Per
            dt.Rows.Add(r)

        Next
        'Response.Write(sql)
        Return dt
    End Function

    Public Function nPageNumber() As String
        Dim str As String
        str = Me.Report.Globals!PageNumber.ToString()
        Return str
    End Function

End Class
